import { getAjax, postAjax, putAjax, deleteAjax } from '@/utils/request'
import { Mutation, Action } from 'vuex'

export interface TreeSelect{
  id: number;
  label: string;
  children?: Array<TreeSelect>;
}
export interface Menu {
  menuId: number | null;
  menuName: string | null;
  parentId: number | null;
  roleSort: number | null;
  orderNum: number | null;
  path: string | null;
  componentName: string| null;
  component: string| null;
  isFrame: boolean | null;
  isCache: boolean | null;
  menuType: string | null;
  visible: boolean | null;
  enable: boolean | null;
  perms: string | null;
  icon: string | null;
  remark: string | null;
}

export interface StateType {
  data: Menu;
}

export interface ModuleType {
  namespaced: boolean;
  state: StateType;
  mutations: {
    SET_DATA: Mutation<StateType>;
  };
  actions: {
    getMenuList: Action<StateType, StateType>;
    getRouters: Action<StateType, StateType>;
    getTreeSelect: Action<StateType, StateType>;
    roleMenuTreeSelect: Action<StateType, StateType>;
    addMenu: Action<StateType, StateType>;
    updateMenu: Action<StateType, StateType>;
    getMenu: Action<StateType, StateType>;
    deleteMenu: Action<StateType, StateType>;
  };
}

const storeModel: ModuleType = {
  namespaced: true,
  state: {
    data: {
      menuId: -1,
      menuName: null,
      parentId: null,
      roleSort: 0,
      orderNum: 0,
      path: null,
      componentName: '',
      component: '',
      isFrame: false,
      isCache: true,
      menuType: '',
      visible: true,
      enable: true,
      perms: '',
      icon: '',
      remark: ''
    }
  },
  mutations: {
    SET_DATA: (state, data) => {
      state.data = data
    }
  },
  actions: {
    getMenuList(_, data) {
      return getAjax('/system/menu/list', data)
    },
    getRouters() {
      return getAjax('/getRouters', null)
    },
    getTreeSelect(_, data) {
      return getAjax('/system/menu/getTreeSelect', data)
    },
    roleMenuTreeSelect(_, roleId) {
      return getAjax(`/system/menu/roleMenuTreeSelect/${roleId}`, null)
    },
    addMenu(_, data) {
      return postAjax('/system/menu/', data)
    },
    updateMenu(_, data) {
      return putAjax('/system/menu/', data)
    },
    getMenu(_, menuId) {
      return getAjax(`/system/menu/${menuId}`, null)
    },
    deleteMenu(_, menuId) {
      return deleteAjax(`/system/menu/${menuId}`, null)
    }
  }
}

export default storeModel
